package com.zc.framework.datasource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * Created by zc on 2016/8/30.
 */
@Component("dsAspectBean")
public class DataSourceAop {

    private static Logger logger = LogManager.getLogger(DataSourceContextHolder.class);

    public DataSourceAop() {
    }

    @Before("@annotation(dynamicDataSource)")
    public void injectDataSource(JoinPoint jp, DynamicDataSource dynamicDataSource) {
        logger.debug("injectDataSource:" + dynamicDataSource.value());
        DataSourceContextHolder.setDataSource(dynamicDataSource.value());
    }

    @After("@annotation(dynamicDataSource)")
    public void restDataSource(JoinPoint jp, DynamicDataSource dynamicDataSource) {
        logger.debug("restDataSource:" + dynamicDataSource.value());
        DataSourceContextHolder.clearDataSource();
    }
}
